<template>
  <div class="add-task">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加新任务</span>
      </div>
      <el-form :model="taskForm" @submit.native.prevent="addTask" label-width="100px">
        <el-form-item label="任务名称">
          <el-input v-model="taskForm.name" placeholder="请输入任务名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addTask">添加任务</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      taskForm: {
        name: '',
        // 其他任务属性
      }
    };
  },
  methods: {
    addTask() {
      const token = localStorage.getItem('token');
      axios.post('/api/add', this.taskForm, {
        headers: {
          'Authorization': token
        }
      })
      .then(response => {
        // 处理成功响应
        if (response.data.code === 1) {
          this.$message.success('任务添加成功');
          // 清空表单
          this.taskForm.name = '';
          // 其他操作，如更新任务列表等
        } else {
          // 处理失败响应
          this.$message.error(response.data.msg);
        }
      })
      .catch(error => {
        // 处理请求错误
        this.$message.error('添加任务失败');
        console.error('Error:', error);
      });
    }
  }
};
</script>

<style scoped>
.add-task {
  margin: 20px;
}
.box-card {
  width: 300px;
}
</style>
